﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaseDal;
using Models;

namespace BaseBll
{
    public class PurchasePayApplyBll
    {
        private static readonly Lazy<PurchasePayApplyBll> _instance = new Lazy<PurchasePayApplyBll>(() => new PurchasePayApplyBll());

        public static PurchasePayApplyBll Instance { get; } = _instance.Value;

        private PurchasePayApplyBll()
        { }

        // 获取单例实例
        private readonly PurchasePayApplyDal DalInstance = PurchasePayApplyDal.Instance;

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DalInstance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询分页数据
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public DataTable GetPageList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return DalInstance.GetPageList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<PurchasePayApplyHead> GetList(string strWhere)
        {
            return DalInstance.GetList(strWhere);
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PurchasePayApplyHead GetModelById(decimal id)
        {
            return DalInstance.GetModelById(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool AddPurchasePayApply(PurchasePayApplyHead model)
        {
            return DalInstance.AddPurchasePayApply(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool UpdatePurchasePayApply(PurchasePayApplyHead model)
        {
            return DalInstance.UpdatePurchasePayApply(model);
        }

        /// <summary>
        /// 删除一条数据，及子表所有相关数据
        /// </summary>
        public bool DeletePurchasePayApply(decimal ID)
        {
            return DalInstance.DeletePurchasePayApply(ID);
        }

        /// <summary>
        /// 修改状态（0：审批中；1：已审批；2：暂存）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateStatus(int id, int status)
        {
            return DalInstance.UpdateStatus(id, status);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(PurchasePayApplyHead model, ref string msg)
        {
            string strWhere;
            if (model.ID > 0)
            {
                strWhere = " and PPA_Number='" + model.PPA_Number + "' and ID !=" + model.ID;
            }
            else
            {
                strWhere = " and PPA_Number='" + model.PPA_Number + "'";
            }
            if (IsExist(strWhere))
            {
                msg = "付款编号已存在";
                return false;
            }

            // 获取公司名称
            Department company = DeptBll.Instance.GetModelById(model.PPA_CompanyId);
            if (company != null)
            {
                model.PPA_CompanyName = company.FullName;
            }
            else
            {
                msg = "所选公司不存在";
                return false;
            }

            // 获取部门名称
            Department dept = DeptBll.Instance.GetModelById(model.PPA_DeptId);
            if (dept != null)
            {
                model.PPA_DeptName = dept.FullName;
            }
            else
            {
                msg = "所选部门不存在";
                return false;
            }

            // 获取供应商名称
            Supplier supplier = SupplierBll.Instance.GetModelById(model.PPA_SupplierId);
            if (supplier != null)
            {
                model.PPA_SupplierName = supplier.S_Name;
                model.PPA_Bank = supplier.S_Bank;
                model.PPA_BankNumber = supplier.S_BankNumber;
            }
            else
            {
                msg = "所选供应商不存在";
                return false;
            }

            return true;
        }

    }
}
